<template>
  <div id="app">
    <el-form label-width="80px">
      <el-form-item label="SPU名称">{{ spu.spuName }} </el-form-item>
      <el-form-item label="SKU名称">
        <el-input placeholder="SKU名称" v-model="SkuInfo.skuName"> </el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input
          placeholder="价格(元)"
          type="number"
          v-model="SkuInfo.price"
        ></el-input>
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input
          placeholder="重量(千克)"
          type="number"
          v-model="SkuInfo.weight"
        ></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input
          placeholder="规格描述"
          type="textarea"
          rows="4"
          v-model="SkuInfo.skuDesc"
        ></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-form inline>
          <el-form-item
            :label="attr.attrName"
            v-for="attr in attrInfoList"
            :key="attr.id"
          >
            <!-- 把ID收集到自身身上 因为这样子数据多得话不需要一直写 -->
            <el-select placeholder="请选择" v-model="attr.attrIdAndValueId">
              <el-option
                :value="`${attr.id}:${attrValue.id}`"
                :label="attrValue.valueName"
                v-for="attrValue in attr.attrValueList"
                :key="attrValue.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>

      <el-form-item label="销售属性">
        <el-form>
          <el-form-item
            :label="sale.saleAttrName"
            v-for="sale in spuSaleAttrList"
            :key="sale.id"
          >
            <el-select placeholder="请选择" v-model="sale.saleIdAndValueId">
              <el-option
                :value="`${sale.id}:${saleValue.id}`"
                :label="saleValue.saleAttrValueName"
                v-for="saleValue in sale.spuSaleAttrValueList"
                :key="saleValue.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <!--  -->
      <el-form-item label="图片列表">
        <!-- @selection-change当选择项发生变化时会触发该事件 -->
        <el-table border :data="spuImageList" @selection-change="imgChange">
          <el-table-column width="80px" type="selection"></el-table-column>
          <el-table-column label="图片" prop="">
            <template slot-scope="{ row }">
              <img
                :src="row.imgUrl"
                :alt="row.imgName"
                style="width: 100px; height: 100px"
              />
            </template>
          </el-table-column>
          <el-table-column label="名称" prop="imgName"></el-table-column>
          <el-table-column label="操作" prop="">
            <template slot-scope="{ row }">
              <el-button
                type="primary"
                v-if="row.isDefault == 0"
                @click="changDefault(row)"
                >设为默认</el-button
              >
              <el-button v-else>默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addSaveInfo">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {
  getImgList,
  getSaleList,
  getattrInfoList,
  addSave,
} from "@/api/product/spu";

export default {
  name: "addSKU",
  data() {
    return {
      spuImageList: [], //存储图片的信息
      spuSaleAttrList: [], //存储销售属性的数据
      attrInfoList: [], //存储平台属性的数据 规格描述
      SkuInfo: {
        // 父组件传来可以直接使用得数据
        category3Id: 0, //三级ID
        spuId: 0, // 当前得id
        tmId: 0, //品牌ID
        // 通过V-model来收集数据
        skuName: "", //SKU得名称
        skuDesc: "", //SKU描述
        skuDefaultImg: "", // 默认图片
        price: "", // 价格
        weight: "", //重量
        skuAttrValueList: [
          // {
          //   attrId: 0,
          //   valueId: 0,
          // },
        ],
        skuImageList: [
          // {
          // imgName
          //   imgUrl: "string",
          //   isDefault: "string",
          //   spuImgId: 0,
          // },
        ],
        skuSaleAttrValueList: [
          // {
          //   saleAttrId: 0,
          //   saleAttrValueId: 0,
          // },
        ],
      },
      // 全部得SPU值
      spu: {},
      // 当点击选择的时候把图片放进来
      changImgList: [],
    };
  },
  mounted() {},
  methods: {
    async getData(row, id) {
      //   console.log("发送请求");
      // 获取图片信息
      let imgres = await getImgList(row.id);
      if (imgres.code == 200) {
        let list = imgres.data;
        list.forEach((item) => {
          item.isDefault = 0;
        });
        this.spuImageList = list;
      }
      // 获取销售属性数据
      let SaleList = await getSaleList(row.id);
      if (SaleList.code == 200) {
        this.spuSaleAttrList = SaleList.data;
      }
      // 获取平台属性数据
      let attrInfoListres = await getattrInfoList(
        id.item1Id,
        id.item2Id,
        id.item3Id
      );
      if (attrInfoListres.code == 200) {
        this.attrInfoList = attrInfoListres.data;
      }
      // console.log(id);
      // 吧三级ID和品牌id和当前得ID给到收集得数据
      this.spu = row;
      this.SkuInfo.spuId = row.id;
      this.SkuInfo.tmId = row.tmId;
      this.SkuInfo.category3Id = id.item3Id;
    },
    // 取消返回的
    cancel() {
      this.$emit("fanhui", { num: 0, flag: "" });
      Object.assign(this._data, this.$options.data());
    },
    // 点击默认的时候
    changDefault(row) {
      this.spuImageList.forEach((item) => {
        item.isDefault = 0;
      });
      row.isDefault = 1;
      this.SkuInfo.skuDefaultImg = row.imgUrl;
    },
    // 当选择项发生改变
    imgChange(data) {
      // console.log(data);
      this.changImgList = data;
    },
    // 保存的时候
    async addSaveInfo() {
      // 整理数据
      let { spuSaleAttrList, attrInfoList, SkuInfo } = this;
      // 先整理销售属性和规格描述
      // 规格描述
      let arr = [];
      attrInfoList.forEach((item) => {
        // 判断选择了的
        if (item.attrIdAndValueId) {
          let [attrId, valueId] = item.attrIdAndValueId.split(":");
          let obj = { attrId, valueId };
          arr.push(obj);
        }
      });
      this.SkuInfo.skuAttrValueList = arr;
      // 销售属性
      let saleArr = [];
      spuSaleAttrList.forEach((item) => {
        // 判断选择了的
        if (item.saleIdAndValueId) {
          let [saleAttrId, saleAttrValueId] = item.saleIdAndValueId.split(":");
          let obj = { saleAttrId, saleAttrValueId };
          saleArr.push(obj);
        }
      });
      this.SkuInfo.skuSaleAttrValueList = saleArr;

      // 整理图片
      this.SkuInfo.skuImageList = this.changImgList.map((item) => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id,
        };
      });

      // 发送请求
      let res = await addSave(this.SkuInfo);
      console.log(res);
      if (res.code == 200) {
        this.$message.success("保存成功");
        this.$emit("fanhui", { num: 0, flag: "" });
        Object.assign(this._data, this.$options.data());
      }
    },
  },
};
</script>
